<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="正则表达式使用"><meta name="keywords" content="工具"><meta name="author" content="kyren"><meta name="copyright" content="kyren"><title>正则表达式使用 | Kyren's Blog</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cf87fcdbd9ab7cf93d894f7c219ae78f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script src="https://v1.hitokoto.cn/?encode=js&amp;charset=utf-8&amp;select=.footer_custom_text" defer></script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '6.2.0'
} </script><meta name="generator" content="Hexo 6.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%A81-%E5%B8%B8%E7%94%A8%E7%9A%84%E5%85%83%E5%AD%97%E7%AC%A6"><span class="toc-number">1.</span> <span class="toc-text">表1.常用的元字符</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%87%8D%E5%A4%8D"><span class="toc-number">1.1.</span> <span class="toc-text">重复</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%A1%A8-2-%E5%B8%B8%E7%94%A8%E7%9A%84%E9%99%90%E5%AE%9A%E7%AC%A6%EF%BC%88%E9%87%8F%E8%AF%8D%EF%BC%89"><span class="toc-number">1.2.</span> <span class="toc-text">表 2.常用的限定符（量词）</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%A1%A83-%E5%B8%B8%E7%94%A8%E7%9A%84%E5%8F%8D%E4%B9%89%E4%BB%A3%E7%A0%81"><span class="toc-number">1.3.</span> <span class="toc-text">表3.常用的反义代码</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%94%9A%E7%82%B9"><span class="toc-number"></span> <span class="toc-text">锚点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E7%B1%BB"><span class="toc-number"></span> <span class="toc-text">字符类</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B7%A5%E5%85%B7"><span class="toc-number">0.1.</span> <span class="toc-text">工具</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8F%82%E8%80%83%EF%BC%9A"><span class="toc-number">0.2.</span> <span class="toc-text">参考：</span></a></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://avatars.githubusercontent.com/u/5827823?v=4"></div><div class="author-info__name text-center">kyren</div><div class="author-info__description text-center">huluo的技术博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://github.com/huluo666">Follow Me</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">239</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">41</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">29</span></a></div><hr><div class="author-info-links"><div class="author-info-links__title text-center">Links</div><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:9090/">Jira</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:8090/">Confluence</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://bloc.leniugame.com/">Leniu</a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">Kyren's Blog</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">主页</a><a class="site-page" href="/essay">随笔</a><a class="site-page" href="/archives">归档</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a><a class="site-page" href="/about">关于</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">正则表达式使用</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2017-03-20</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/%E5%B7%A5%E5%85%B7/">工具</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">小写  a-z</span><br><span class="line">大写  A-Z</span><br><span class="line">汉字  \u4E00-\u9FA5</span><br><span class="line">数字  \u0030-\u0039</span><br></pre></td></tr></table></figure>

<p>正则表达式”[a-zA-Z]+”代表什么含义?</p>
<p>a-z是所有小写字母 A-Z是所有大写字母<br>[a-zA-Z]+ 这个就是一个不分大小写字母的英文单词（无限字母） 最少是一个字母</p>
<h4 id="表1-常用的元字符"><a href="#表1-常用的元字符" class="headerlink" title="表1.常用的元字符"></a>表1.常用的元字符</h4><table>
<thead>
<tr>
<th>代码</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><code>.</code></td>
<td>匹配除换行符以外的任意字符</td>
</tr>
<tr>
<td><code>\w</code></td>
<td>匹配字母或数字或下划线或汉字  —-<code>\W</code>大写W匹配不是字母、数字、下划线的字符</td>
</tr>
<tr>
<td><code>\s</code></td>
<td>匹配任意的空白符  等同于<code>[\f\n\r\t\v]</code>;</td>
</tr>
<tr>
<td>\d</td>
<td>匹配数字   相当于<code>[0-9]</code>    —– <code>\D</code> 匹配不是数字的字符</td>
</tr>
<tr>
<td><code>\b</code></td>
<td>匹配单词的开始或结束</td>
</tr>
<tr>
<td><code>^</code></td>
<td>匹配字符串的开始</td>
</tr>
<tr>
<td><code>$</code></td>
<td>匹配字符串的结束</td>
</tr>
<tr>
<td><code>[</code></td>
<td>字符组的起始符号</td>
</tr>
</tbody></table>
<p>如果你想查找元字符本身的话，比如你查找.,或者*,就出现了问题：你没办法指定它们，因为它们会被解释成别的意思。这类特殊字符原本是字母，在表达特殊含义时需要用”&quot;转义。</p>
<p>如：</p>
<table>
<thead>
<tr>
<th>元字符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>*</td>
<td>匹配<code>*</code> 字符</td>
</tr>
<tr>
<td>.</td>
<td>匹配<code>.</code> 字符</td>
</tr>
<tr>
<td>/</td>
<td>匹配 <code>/</code> 字符</td>
</tr>
<tr>
<td>\</td>
<td>匹配 <code>\</code> 字符</td>
</tr>
<tr>
<td>[</td>
<td>匹配 <code>[</code> 字符</td>
</tr>
<tr>
<td>]</td>
<td>匹配 <code>]</code> 字符</td>
</tr>
</tbody></table>
<h5 id="重复"><a href="#重复" class="headerlink" title="重复"></a>重复</h5><p>你已经看过了前面的*,+,{2},{5,12}这几个匹配重复的方式了。下面是正则表达式中所有的限定符(指定数量的代码，例如*,{5,12}等)：</p>
<h5 id="表-2-常用的限定符（量词）"><a href="#表-2-常用的限定符（量词）" class="headerlink" title="表 2.常用的限定符（量词）"></a>表 2.常用的限定符（量词）</h5><table>
<thead>
<tr>
<th>代码&#x2F;语法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><code>*</code></td>
<td>重复零次或更多次  相当于<code>&#123;0,&#125;</code></td>
</tr>
<tr>
<td><code>+</code></td>
<td>重复一次或更多次  相当于<code>&#123;1,&#125;</code></td>
</tr>
<tr>
<td><code>?</code></td>
<td>重复0次或1次  相当于<code>&#123;0,&#125;</code>或<code>&#123;0,1&#125;</code></td>
</tr>
<tr>
<td><code>&#123;n&#125;</code></td>
<td>重复 n 次</td>
</tr>
<tr>
<td><code>&#123;n,&#125;</code></td>
<td>重复 n 次或更多次</td>
</tr>
<tr>
<td><code>&#123;n,m&#125;</code></td>
<td>重复 n 到 m 次</td>
</tr>
</tbody></table>
<h5 id="表3-常用的反义代码"><a href="#表3-常用的反义代码" class="headerlink" title="表3.常用的反义代码"></a>表3.常用的反义代码</h5><p>特殊字符串表达特殊的含义,如果要检测”*”等特殊字符，需要在前面加上\进行转义。</p>
<table>
<thead>
<tr>
<th>代码&#x2F;语法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>\W</td>
<td>匹配任意不是字母，数字，下划线，汉字的字符</td>
</tr>
<tr>
<td>\S</td>
<td>匹配任意不是空白符的字符</td>
</tr>
<tr>
<td>\D</td>
<td>匹配任意非数字的字符</td>
</tr>
<tr>
<td>\B</td>
<td>匹配不是单词开头或结束的位置</td>
</tr>
<tr>
<td>[^x]</td>
<td>匹配除了x以外的任意字符</td>
</tr>
<tr>
<td>[^aeiou]</td>
<td>匹配除了aeiou这几个字母以外的任意字符</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>特殊字符</th>
<th>含义</th>
<th>例子</th>
</tr>
</thead>
<tbody><tr>
<td>^</td>
<td>表示字符串开头</td>
<td>“^2”表示以2开始</td>
</tr>
<tr>
<td>$</td>
<td>表示字符串的结尾，如果允许跨行检索，那么也表示行尾</td>
<td>“2$”表示以2结尾</td>
</tr>
<tr>
<td>[]</td>
<td>中括号表达式，匹配中括号内的任意字符，在中括号的开头加上”^”表示补集</td>
<td>[0-9a-zA-Z]表示\w，[^0-9a-zA-Z]表示\W</td>
</tr>
<tr>
<td>()</td>
<td>表示将括号内分组并标记为子表达式，在开头加上”?:”表示仅分组不标记为子表达式。\2表示重复第二子表达式</td>
<td>“(abc)\1”匹配”abcabc”，”(?:abc)”仅分组不编号</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>|</th>
<th>或，可以连续使用</th>
<th>“x|y|z”匹配 x或 y或 z</th>
</tr>
</thead>
<tbody><tr>
<td>{}</td>
<td>{n}重复前一个表达式 n次，{n,}少于 n次，{n,m}大于 n次小于 m次</td>
<td>“\d{4}”匹配四个数字</td>
</tr>
</tbody></table>
<h3 id="锚点"><a href="#锚点" class="headerlink" title="锚点"></a>锚点</h3><p><strong>锚点</strong>用于匹配一个位置，下列为常用的锚点</p>
<ul>
<li>^ 起始的位置 &#x2F;^http&#x2F;（用在表达式的开头表示一个正则表达是的开始）</li>
<li>$ 结尾的位置 &#x2F;.jpg$&#x2F; （表示字符串的结尾）</li>
<li>\b 单词边界 （匹配单词的开始或结束）</li>
</ul>
<h3 id="字符类"><a href="#字符类" class="headerlink" title="字符类"></a>字符类</h3><p><strong>字符类</strong>用于匹配一类字符中的一个，下面为几个常用的例子，</p>
<ul>
<li>[abc] 可用于匹配 a b 还有 c</li>
<li>[0-9] 可用于匹配一个数字</li>
<li>[^0-9] 可用于匹配一个<strong>非</strong>数字</li>
<li>[a-z] 可用于匹配一个字母</li>
<li>. 任意字符但换行符除外</li>
</ul>
<p>开发常用正则示例详解：</p>
<p>(1).用户名</p>
<p>规则—1.字母开头(第一位字母) —[a-zA-Z] 表示第一位为字母 ;</p>
<p>​	    2.后面几位为数字或下划线—-[a-zA-Z0-9] 表示字符为字母数字或者下滑线; </p>
<p>​           3.位数限制6-16位—{5,15} 表示有5到15个符合[a-zA-Z0-9]的字符</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^[a-zA-Z][a-zA-Z0-9_]&#123;5,15&#125;$</span><br></pre></td></tr></table></figure>



<p>(2).密码</p>
<p>(?![0-9]{0,24}$)  表示前24位不能全为数字<br>\S  表示任何非空白字符(不为空格,回车,tab等输入的空白符)<br>[\S]{6,24} 表示6到24位不为空格的字符</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;^(?![0-9]&#123;0,24&#125;)[\S]&#123;6,24&#125;$&quot; </span><br></pre></td></tr></table></figure>

<p>(3).手机号</p>
<p>1 表示第一位为1<br>[3|4|5|7|8] 表示第二位为 3,4,5,6,7,或者8<br>[0-9]{9} 表示9位任意数字</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^13|4|5|7|8&#123;9&#125;$</span><br></pre></td></tr></table></figure>

<p>(4).email</p>
<p> (格式 用户名@邮箱网站.com   或   用户.名@邮箱网站.com.cn )</p>
<p>[a-zA-Z0-9_-]+ 	表示1位或多位字母数字下划线和连接符<br>(.[a-zA-Z0-9_-]+)* 	表示 0个或多个  由一个点.和一个或多个字母数字下划线连接符组成的字符串</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*@[a-zA-Z0-9_-]+.com(.cn)?$</span><br></pre></td></tr></table></figure>

<p>(5).查找字符串中的数字</p>
<p>this is num 185cm</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\\d+</span><br></pre></td></tr></table></figure>



<p>(6).查找字符串中的Emoji</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">\\[+[a-z]+\\]  </span><br><span class="line">\\[em\\d&#123;4&#125;\\] //QQEmoji</span><br></pre></td></tr></table></figure>



<p>(7).查找字符串中的话题 #iOS开发#</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#([^\\#|]+)#</span><br></pre></td></tr></table></figure>



<p>下面是<code>JavaScript</code>示例代码，利用CoderRunner可以实时编写测试</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">&quot;Thisis a Box! 价格2(¥3.15) 1223#呵呵呵#[偷笑]啊哈[哈哈] https://segmentfault.com/a/ 262 [嘻嘻]&quot;</span>;</span><br><span class="line"><span class="keyword">var</span> numberRex = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;\\d+&quot;</span>);<span class="comment">//匹配数字 +表示至少一位</span></span><br><span class="line"><span class="keyword">var</span> emoji= <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]&quot;</span>);<span class="comment">// 表情的规则--汉子+字母+数字组成</span></span><br><span class="line"><span class="keyword">var</span> emoji2= <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;\\[+[a-z]+\\]&quot;</span>);<span class="comment">// 表情的规则--[cool] 里面为纯字母</span></span><br><span class="line"><span class="keyword">var</span> atPattern= <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;@[0-9a-zA-Z\\u4e00-\\u9fa5]+&quot;</span>);<span class="comment">// @的规则</span></span><br><span class="line"><span class="keyword">var</span> topicPattern= <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;#[0-9a-zA-Z\\u4e00-\\u9fa5]+#&quot;</span>);<span class="comment">// #话题#的规则</span></span><br><span class="line"><span class="keyword">var</span> urlPattern = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;\\b(([\\w-]+://?|www[.])[^\\s()&lt;&gt;]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))&quot;</span>);<span class="comment">// url链接的规则</span></span><br><span class="line"><span class="keyword">var</span> userName = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^[a-zA-Z][a-zA-Z0-9_]&#123;5,15&#125;$&quot;</span>);<span class="comment">// (由6-16位字母数字下划线组成,第一位不能为数字或下划线) </span></span><br><span class="line"><span class="keyword">var</span> passWorld = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^(?![0-9]&#123;0,24&#125;)[\\S]&#123;6,24&#125;$&quot;</span>);<span class="comment">// ( 6-24位,不能为纯数字,不能包含空格) </span></span><br><span class="line"><span class="keyword">var</span> phoneNum  = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^1[3|4|5|7|8][0-9]&#123;9&#125;$&quot;</span>);<span class="comment">// (1开头3，4，5，7，8区分运营商,由于变动较快，限制适当放宽点) 等同^1[3|4|5|7|8][0-9]\\d&#123;8&#125;$</span></span><br><span class="line"><span class="keyword">var</span> phoneNum2 = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^1[3|4|5|7|8][0-9]\\d&#123;8&#125;$&quot;</span>);<span class="comment">//\d&#123;8&#125; 相当于[0-9]&#123;8&#125; </span></span><br><span class="line"><span class="keyword">var</span> phoneNum3 = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^1[3|4|5|7|8]\\d&#123;9&#125;$&quot;</span>);<span class="comment">//所以可以精简为这个</span></span><br><span class="line"><span class="keyword">var</span> validateNum = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^[0-9]+$&quot;</span>);<span class="comment">// 验证是否纯数字- ^表示开头 +表示至少1次 $表示结束</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> <span class="title class_">Email1</span> = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]&#123;2,4&#125;&quot;</span>);<span class="comment">//用户名@邮箱网站.com   或   用户.名@邮箱网站.com.cn </span></span><br><span class="line"><span class="keyword">var</span> <span class="title class_">Email2</span> = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\.\\w+([-.]\\w+)*$&quot;</span>);<span class="comment">//利用元字符\w对上面正则简写</span></span><br><span class="line"><span class="keyword">var</span> <span class="title class_">IdCard</span> = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;(^[1-9]\\d&#123;7&#125;((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d&#123;3&#125;$)|(^[1-9]\\d&#123;5&#125;[1-9]\\d&#123;3&#125;((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d&#123;4&#125;)|\\d&#123;3&#125;[Xx])$)&quot;</span>);<span class="comment">//匹配身份证号码</span></span><br><span class="line"><span class="keyword">var</span> price = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;¥(\\d+(?:\\.\\d+)?)&quot;</span>);<span class="comment">//匹配价格 只考虑人民币()表示子表达式 ¥3.15</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">match</span>(numberRex));</span><br><span class="line"><span class="comment">//PS：元字符注意加转义符，正则表达式中&#x27;\d&#x27;可以看作元字符，或者元字符序列，是一个完整的整体；但是因为Object-C语言中‘\&#x27;已经有了它的特殊意义了，所以&#x27;\b&#x27;会被先转义，所以就无法得到正则表达式真正想要的内容。</span></span><br></pre></td></tr></table></figure>

<h5 id="工具"><a href="#工具" class="headerlink" title="工具"></a>工具</h5><p><a target="_blank" rel="noopener" href="http://tool.oschina.net/regex">http://tool.oschina.net/regex</a></p>
<p><a target="_blank" rel="noopener" href="http://tool.lu/regex">http://tool.lu/regex</a></p>
<p>正则网上内容基本一样，关键在于理解掌握，下面是一些比较好的教程。</p>
<h5 id="参考："><a href="#参考：" class="headerlink" title="参考："></a>参考：</h5><p><a target="_blank" rel="noopener" href="http://www.ctolib.com/topics-102627.html">http://www.ctolib.com/topics-102627.html</a></p>
<p><a target="_blank" rel="noopener" href="http://www.ctolib.com/topics-34117.html">http://www.ctolib.com/topics-34117.html</a></p>
<p><a target="_blank" rel="noopener" href="https://www.zybuluo.com/demonly/note/443320">https://www.zybuluo.com/demonly/note/443320</a></p>
<p><a target="_blank" rel="noopener" href="https://yq.aliyun.com/ziliao/99994?spm=5176.8246799.0.0.tuxn2y">https://yq.aliyun.com/ziliao/99994?spm=5176.8246799.0.0.tuxn2y</a></p>
<p><a target="_blank" rel="noopener" href="http://www.infoq.com/cn/news/2011/01/regular-expressions-1">http://www.infoq.com/cn/news/2011/01/regular-expressions-1</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">kyren</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://huluo666.gitee.io/2017/03/20/正则表达式使用/">http://huluo666.gitee.io/2017/03/20/正则表达式使用/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://huluo666.gitee.io">Kyren's Blog</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%B7%A5%E5%85%B7/">工具</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2017/03/22/iOS%E8%87%AA%E5%AE%9A%E4%B9%89Log/"><i class="fa fa-chevron-left">  </i><span>iOS开发自定义NSLog</span></a></div><div class="next-post pull-right"><a href="/2017/03/19/shell%E8%87%AA%E5%8A%A8%E6%89%93%E5%8C%85IPA/"><span>shell自动打包IPA</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2022 By kyren</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="footer_custom_text">hitokoto</div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/lib/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>